// @ts-ignore
import { ref, reactive } from 'vue'
// @ts-ignore
import { ElMessage } from 'element-plus'
// @ts-ignore
import * as systemApi from '../api/system'
// @ts-ignore
import type { OperationLog, LogQueryParams } from '../api/system'

// @ts-ignore
export const useOperationLog = () => {
  // 操作日志相关的响应式数据
  // @ts-ignore
  const loading = ref(false)
  // @ts-ignore
  const selectedLogs = ref([])
  // @ts-ignore
  const tableData = ref([])
  // @ts-ignore
  const pagination = reactive({
    page: 1,
    limit: 20,
    total: 0
  })

  // 详情数据
  // @ts-ignore
  const detailLog = ref({
    id: 0,
    userId: 0,
    userName: '',
    action: '',
    module: '',
    description: '',
    ip: '',
    userAgent: '',
    requestData: '',
    responseData: '',
    createdAt: ''
  })

  // 查询参数
  // @ts-ignore
  const queryParams = reactive({
    page: 1,
    limit: 20,
    userId: undefined,
    action: '',
    module: '',
    startDate: '',
    endDate: ''
  })

  // 加载日志列表
  // @ts-ignore
  const loadLogs = async () => {
    // @ts-ignore
    loading.value = true
    try {
      const params: LogQueryParams = {
        page: pagination.page,
        limit: pagination.limit,
        userId: queryParams.userId,
        action: queryParams.action,
        module: queryParams.module,
        startDate: queryParams.startDate,
        endDate: queryParams.endDate
      }
      
      const response = await systemApi.getOperationLogs(params)
      
      if (response.data && response.data.list) {
        // @ts-ignore
        tableData.value = response.data.list
        // @ts-ignore
        pagination.total = response.data.pagination.total
      }
    } catch (error: any) {
      console.error('加载日志数据失败:', error)
      ElMessage.error('加载日志数据失败: ' + (error.message || '未知错误'))
    } finally {
      // @ts-ignore
      loading.value = false
    }
  }

  // 搜索日志
  // @ts-ignore
  const handleSearch = () => {
    // @ts-ignore
    pagination.page = 1
    loadLogs()
  }

  // 重置搜索条件
  // @ts-ignore
  const handleReset = () => {
    Object.assign(queryParams, {
      page: 1,
      limit: 20,
      userId: undefined,
      action: '',
      module: '',
      startDate: '',
      endDate: ''
    })
    loadLogs()
  }

  // 处理选择变化
  // @ts-ignore
  const handleSelectionChange = (selection: any[]) => {
    // @ts-ignore
    selectedLogs.value = selection
  }

  // 显示日志详情
  // @ts-ignore
  const showLogDetail = async (log: any) => {
    try {
      const response = await systemApi.getOperationLog(log.id)
      if (response.data) {
        // @ts-ignore
        detailLog.value = response.data
        // @ts-ignore
        return detailLog.value
      }
    } catch (error: any) {
      ElMessage.error('获取日志详情失败: ' + (error.message || '未知错误'))
      throw error
    }
  }

  // 删除日志
  // @ts-ignore
  const deleteLog = async (log: any) => {
    try {
      await systemApi.deleteOperationLog(log.id)
      ElMessage.success('删除成功')
      loadLogs()
      return true
    } catch (error: any) {
      ElMessage.error('删除失败: ' + (error.message || '未知错误'))
      throw error
    }
  }

  // 分页相关方法
  // @ts-ignore
  const handleSizeChange = (val: number) => {
    // @ts-ignore
    pagination.limit = val
    // @ts-ignore
    pagination.page = 1
    loadLogs()
  }

  // @ts-ignore
  const handleCurrentChange = (val: number) => {
    // @ts-ignore
    pagination.page = val
    loadLogs()
  }

  return {
    // 数据
    // @ts-ignore
    loading,
    // @ts-ignore
    selectedLogs,
    // @ts-ignore
    tableData,
    // @ts-ignore
    pagination,
    // @ts-ignore
    detailLog,
    // @ts-ignore
    queryParams,
    
    // 方法
    // @ts-ignore
    loadLogs,
    // @ts-ignore
    handleSearch,
    // @ts-ignore
    handleReset,
    // @ts-ignore
    handleSelectionChange,
    // @ts-ignore
    showLogDetail,
    // @ts-ignore
    deleteLog,
    // @ts-ignore
    handleSizeChange,
    // @ts-ignore
    handleCurrentChange
  }
}